(function ($) {

    phoneTime = 60;
    checkflag = {
        nameflag: false,
        passflag: false,
        passflag2: false,
        phoneflag: false
    };

    var LoginFlag = false;
    jigsaw.init(document.getElementById('captcha'), function () {
        LoginFlag = true;
        document.getElementById('msg').innerHTML = '验证成功！'
    });


    register = function () {
        var password = $("#register-pass").val();
        password = hex_md5(password);
        var code = $("#code").val();
        var sex = $("input[name='sex']").val();
        if (checkInput()) {
            if (code.length != 0) {
                $.ajax({
                    url: Store.context + "/user/register.do",
                    type: "post",
                    data: {
                        username: $("#register-username").val(),
                        password: password,
                        sex: sex,
                        phone: $("#phone").val(),
                        code: code
                    },
                    success: function (data) {
                        if (data.status == "1") {
                            if (data.msg == "-1") {
                                alert("验证码错误");
                            }
                            if (data.msg == "-2") {
                                alert("注册中出错");
                            }
                        }
                        if (data.status == "0") {
                            alert("注册成功");
                            window.location.href = Store.context +"/index.do";
                        }
                    }
                })
            } else {
                alert('请填写验证码');
            }
        } else {
            alert("请填写完整信息")
        }

    }

    sub = function () {
        if (LoginFlag == true) {
            if (checkLogin()) {
                var password = $("#password").val();
                password = hex_md5(password);
                $.ajax({
                    url: Store.context  + "/user/login.do",
                    type: "post",
                    data: {
                        username: $("#username").val(),
                        password: password,
                    },
                    success: function (d) {
                        if (d.status == 1) {
                            // prompt("用户名密码错误",1000);
                            alert("用户名密码错误")
                        } else {
                            location.href = Store.context + "/index.do";
                        }
                    }
                })
            }
        } else {
            alert("请先验证图片");
        }
    }

    forgetPassword = function () {
        var form_for = document.createElement("form");
        form_for.action = Store.context + "/user/gotoForgetPassword.do";
        form_for.method = "post";
        document.body.appendChild(form_for);
        var input1 = document.createElement("input");
        input1.name = "page";
        input1.value = 1;
        form_for.appendChild(input1);
        form_for.submit();
        document.body.removeChild(form_for);
    }

    closeModal = function () {
        $("#Modal1").hide();
    }

    $(".tab").click(function () {
        $("input[type='text']").val("");
        $("input[type='password']").val("");
    })


    function prompt(text, _time) {
        var windowWidth = $(window).width();
        var tipsDiv = '<div class="tipsClass">' + text + '</div>';
        $('body').append(tipsDiv);
        $('div.tipsClass').css({
            'z-index': 999,
            'top': 130 + 'px',
            'left': ( windowWidth / 2 ) - 350 / 2 + 'px',
            'position': 'fixed',
            'padding': '10px 16px',
            'background': 'red',
            'font-size': 18 + 'px',
            'margin': '0 auto',
            'text-align': 'center',
            'width': '350px',
            'height': 'auto',
            'color': 'white',
            'opacity': '0.8'
        }).show();
        setTimeout(function () {
            $('div.tipsClass').fadeOut();
        }, _time);
    }

    checkLogin = function () {
        var flag = true;
        var username = $("#username").val();
        var password = $("#password").val();
        if (username.length == 0) {
            alert("用户名不能为空");
            flag = false;
        }
        if (password.length == 0) {
            alert("密码不能为空");
            flag = false;
        }
        return flag;
    }

    checkInput = function () {
        if (!checkflag.nameflag || !checkflag.passflag ||
            !checkflag.passflag2 || !checkflag.phoneflag) {
            return false;
        }
        return true;
    }
    //    用户名规范
    checkName = function checkName() {
        var _thisMsg = $(".checkname");
        var msgI = _thisMsg.children("i");
        var nameinput = $("#register-username");
        var username = nameinput.val();

        var rule1 = /^[a-zA-Z][a-zA-Z0-9_]*$/;
        if (username.length == 0) {
            msgerror(msgI, nameinput, "用户名不能为空");
            checkflag.nameflag = false;
            _thisMsg.show();
        } else if (username.indexOf(" ") != -1) {
            msgerror(msgI, nameinput, "用户名不能存在空格");
            checkflag.nameflag = false;
            _thisMsg.show();
        } else if (!rule1.test(username)) {
            msgerror(msgI, nameinput, "用户名不符合规范");
            checkflag.nameflag = false;
            _thisMsg.show();
        } else {
            $.ajax({
                url: Store.context +"/user/check.do",
                type: "post",
                data: {
                    username: username
                },
                success: function (data) {
                    if (data.status == 0) {
//                    success
                        msgsuccess(msgI, nameinput, "该用户名可以使用");
                        checkflag.nameflag = true;
                        _thisMsg.show();
                    } else {
//                    error
                        msgerror(msgI, nameinput, "该用户名已被占用");
                        checkflag.nameflag = false;
                        _thisMsg.show();
                    }
                }
            })
        }

    }
    //     确认密码
    confirmPass = function () {
        var pass = $("#register-pass").val();
        var pass2 = $("#register-pass2").val();
        var _thisMsg = $(".confimpass");
        var msgI = _thisMsg.children("i");
        if (pass == pass2) {
            msgI.attr("class", "fa fa-check success");
            checkflag.passflag = true;
            _thisMsg.show();
        } else {
            msgI.attr("class", "fa fa-exclamation-circle error");
            checkflag.passflag = false;
            _thisMsg.show();
        }
    }
    //    密码规范
    checkpass = function () {
        var _thisMsg = $(".checkpass");
        var msgI = _thisMsg.children("i");
        var pass = $("#register-pass").val();
        var passinput = $("#register-pass");
        if (pass.length == 0) {
            msgerror(msgI, passinput, "密码不能为空");
            checkflag.passflag2 = false;
        } else if (pass.indexOf(" ") != -1) {
            msgerror(msgI, passinput, "密码不能含有空格");
            checkflag.passflag2 = false;
        } else {
            checkflag.passflag2 = true;
            msgsuccess(msgI, passinput, "");
        }
    }
    //发送短信验证码
    getPhoneCode = function () {
        var _thismsg = $(".checkphone");
        var msgI = _thismsg.children("i");
        var phoneinput = $("#phone")
        var phone = phoneinput.val();

        var phoneExp = /^1[34578][0-9]{9}$/;//号码规范
        if (!phone || phone === '') {
            checkflag.phoneflag = false;
            msgerror(msgI, phoneinput, "手机号不能为空");
            _thismsg.show();
        } else if (!phoneExp.test(phone)) {
            checkflag.phoneflag = false;
            msgerror(msgI, phoneinput, "请输入正确格式");
            _thismsg.show();
        } else {
            msgsuccess(msgI, phoneinput, "");
            _thismsg.hide();
            $.ajax({
                url: Store.context +"/user/sendPhoneCode.do",
                type: 'post',
                data: {
                    phone: phone,
                },
                success: function (data) {
                    if (data.status == "0") {
                        console.log("没有问题");
                        checkflag.phoneflag = true;
                        time();
                        msgsuccess(msgI, phoneinput, "");
                        _thismsg.hide();
                    } else {
                        checkflag.phoneflag = false;
                        msgerror(msgI, phoneinput, "您输入的手机号已注册")
                        _thismsg.show();
                        return;
                    }
                }
            })
        }
    }
    //时间计算器
    time = function () {
        var _this = this;
        var t = $("#phonetime");
        if (phoneTime == 0) {
            phoneTime = 60;
            t.removeAttr("disabled");
            t.val("获取验证码");
        } else {
            t.attr('disabled', true);
            t.val(phoneTime + "秒后可重新发送");
            phoneTime--;
            setTimeout(function () {
                time();
            }, 1000);
        }
    }
    //显示错误信息
    msgerror = function (data, input, msg) {
        data.attr("class", 'fa fa-exclamation-circle error');
        input.addClass("errorborder");
        data.attr("title", msg);
    }
    //显示成功信息
    msgsuccess = function (data, input, msg) {
        data.attr("class", 'fa fa-check success');
        input.removeClass("errorborder");
        data.attr("title", msg);
    }
}(jQuery))